#include <signal.h>
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <functional>

#include "OssManage.h"
#include "MqUtil.h"

using std::cout;
using std::endl;
using std::string;

using namespace std::literals;

bool UploadFile(const string &path, std::shared_ptr<OssManage> oss)
{
    while(access(path.c_str(), R_OK) == -1)// 可能要轮询检查
    {
#ifdef DEBUG
        cout << "[ERROR] " << "no this file or file cant be read" << endl;
#endif
        std::this_thread::sleep_for(1s);
    }
    if(oss->OssSimpleUploadLocal(path) == -1)
    {
#ifdef DEBUG
        cout << "[ERROR] " << "oss upload failed" << endl;
#endif
        return false;
    }
    return true; 
}

void sigHandler(int signum)
{
    cout << "正在退出" << endl;
    sleep(1);
    exit(0);
}

int main()
{
    signal(SIGINT, sigHandler);

    string EndPoint = "https://oss-cn-wuhan-lr.aliyuncs.com";    
    string Region = "cn-wuhan-lr";
    string BucketName = "playba-test";
    string OriginPath = "userfile/";
    std::shared_ptr<OssManage> oss = OssManage::getInstance();

    oss->InitOssClient(EndPoint, Region, BucketName, OriginPath);
    string uri = "amqp://guest:guest@localhost:5672/%2F";
    const string &queue = "queue1";
    MqConsumer consumer{uri, queue};

    consumer.ConsumeLoop(bind(UploadFile, std::placeholders::_1, std::ref(oss)));

}